var b1race = [],b2race = [],c1race = [],c2race = [],c3race = [];
var dataJSON = [];

$(function (){
	$("#types").val(sessionStorage.getItem('types'));

	$.ajax({
		cache : true,
		type : "get",
		url : "/jzApi/examinations/selExaminationComAll3",
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {

			if (data.code == 0) {

				$('#cbatchName').selectPage({
					showField: 'name',
					keyField: 'id',
					data: data.data,
					eAjaxSuccess : function(d){
						var result;
						if(d) result = d.data;
						else result = undefined;
						return result;
					},
					eSelect : function(data){
						$('#cbatchName').val(data.id);
					}
				});

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});

})


function save() {
	// validateRule();

	if( !$("#name").val() ){
		layer.msg('请输入竞赛名称')
		return false;
	}

	if( !$("#type").val() ){
		layer.msg('请选择竞赛方式')
		return false;
	}

	if( !$("#serial").val() ){
		layer.msg('请输入项目编号')
		return false;
	}

	if( !$("#startTime").val() ){
		layer.msg('请选择考试开始时间')
		return false;
	}

	if( !$("#endTime").val() ){
		layer.msg('请选择考试结束时间')
		return false;
	}

	if( b1race.length ){
		b1race = b1race.map(function (v){
			return v.id;
		})
		// b1race.unshift(dataJSON.data.b1[0].id);
		b1race = b1race.join(',').replace(/,/g,';;');
		$("#b1raceInput").val(b1race);
	}

	if( b2race.length ){
		b2race = b2race.map(function (v){
			return v.id;
		})
		// b2race.unshift(dataJSON.data.b2[0].id);
		b2race = b2race.join(',').replace(/,/g,';;');
		$("#b2raceInput").val(b2race);
	}

	if( c1race.length ){
		c1race = c1race.map(function (v){
			return v.id;
		})
		// c1race.unshift(dataJSON.data.c1[0].id);
		c1race = c1race.join(',').replace(/,/g,';;');
		$("#c1raceInput").val(c1race);
	}

	if( c2race.length ){
		c2race = c2race.map(function (v){
			return v.id;
		})
		// c2race.unshift(dataJSON.data.c2[0].id);
		c2race = c2race.join(',').replace(/,/g,';;');
		$("#c2raceInput").val(c2race);
	}

	if( c3race.length ){
		c3race = c3race.map(function (v){
			return v.id;
		})
		// c2race.unshift(dataJSON.data.c2[0].id);
		c3race = c3race.join(',').replace(/,/g,';;');
		$("#c3raceInput").val(c3race);
	}

	if( $("input[name='refereeB']").val() ){

		let refereeB = $("input[name='refereeB']").val();
			refereeB = refereeB.replace(/,/g,';;');
			$("input[name='refereeB']").val(refereeB)
	}

	if( $("input[name='refereeC']").val() ){

		let refereeC = $("input[name='refereeC']").val();
			refereeC = refereeC.replace(/,/g,';;');
			$("input[name='refereeC']").val(refereeC)
	}

	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/examinationBatchCom/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			type : {
				required : true
			},
			serial : {
				required : true
			},
			startTime : {
				required : true
			},
			endTime : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入竞赛名称"
			},
			type : {
				required : icon + "请选择竞赛方式"
			},
			serial:{
				required : icon + "请输入项目编号"
			},
			startTime:{
				required : icon + "请选择考试开始时间"
			},
			endTime:{
				required : icon + "请选择考试结束时间"
			}
		}
	})
}

layui.use(['laydate','xmSelect'],function (){
	var laydate = layui.laydate;
	var xmSelect = layui.xmSelect;

	laydate.render({
		elem: '#startTime'
		,type: 'datetime'
	});
	laydate.render({
		elem: '#endTime'
		,type: 'datetime'
	});

	xmSelectData();

})

async function xmSelectData(){
	dataJSON = await knowledg();

	var daArr = ['b1','b2','c1','c2'];
	daArr.forEach(function (response){
		dataJSON.data[response].forEach(function (v){
			v.children.forEach(function (val,k){
				val.selected = false;
				val.value = k;
			})
		})
	})

	const b1 = [{name:'婴幼儿保育技能实操'}], b2 = [{name:'模拟教学'}] , c1 = [{name:'婴幼儿早期学习支持'}], c2 = [{name:'集体教学活动设计'}], c3 = [{name:'说课'}];

	//选择模块题目
	//B1
	xmSelect.render({
		el: '#b1race',
		autoRow: true,
		paging: true,
		pageSize: 20,
		height: '300px',
		name:'',
		tree: {
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
			clickExpand:true,
			clickCheck: true
		},
		data(){
			// return dataJSON.data.b1
			return b1;
		},
		on:function (data){
			b1race = data.arr
		}
	})

	//B2
	xmSelect.render({
		el: '#b2race',
		autoRow: true,
		paging: true,
		pageSize: 20,
		height: '300px',
		name:'',
		tree: {
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
			clickExpand:true,
			clickCheck: true
		},
		data(){
			// return dataJSON.data.b2
			return b2
		},
		on:function (data){
			console.log(11111,data);
			if( data.arr.length ){
				b2race = data.arr
			}
		}
	})

	//C1
	xmSelect.render({
		el: '#c1race',
		autoRow: true,
		paging: true,
		pageSize: 20,
		height: '300px',
		name:'',
		tree: {
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
			clickExpand:true,
			clickCheck: true
		},
		data(){
			// return dataJSON.data.c1
			return c1
		},
		on:function (data){
			c1race = data.arr
		}
	})

	//C2
	xmSelect.render({
		el: '#c2race',
		autoRow: true,
		paging: true,
		pageSize: 20,
		height: '300px',
		name:'',
		tree: {
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
			clickExpand:true,
			clickCheck: true
		},
		data(){
			// return dataJSON.data.c2
			return c2
		},
		on:function (data){
			c2race = data.arr
		}
	})

	//C3
	xmSelect.render({
		el: '#c3race',
		autoRow: true,
		paging: true,
		pageSize: 20,
		height: '300px',
		name:'',
		tree: {
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
			clickExpand:true,
			clickCheck: true
		},
		data(){
			// return dataJSON.data.c2
			return c3
		},
		on:function (data){
			c3race = data.arr
		}
	})

}

function knowledg(){
	return $.ajax({
		cache : true,
		type : "get",
		url : "/jzweb/itemBank/byKnowledgesGet",
		data : {
			cprofileId:'1654692612624175105'
		},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				return data.data
			} else {
				parent.layer.alert(data.msg)
			}

		}
	});
}

examJudge();
function examJudge(){

	// $("#refereeBBox").click(function (ev){
		$.ajax({
			cache: true,
			type: "get",
			url: "/jzweb/examinationBatchCom/getUserReferee",
			data: {
				track: 'A'
			},
			async: false,
			error: function (request) {
				parent.layer.alert("Connection error");
			},
			success: function (data) {

				if (data.code == 0) {
					var format = [];

					format = data.data.map(function (v) {
						if( v ){
							v.selected = false;
							v.value = v.id;
						}
						return v;
					})
					xmSelect.render({
						el: '#refereeBBox',
						autoRow: true,
						name: '',
						max: 5,
						data() {
							return format
						},
						maxMethod(seles, item) {
							layer.msg(`最多只能选择5个`)
						},
						on: function (data) {
							let arr = [];
							data.arr.map(function (v){
								arr.push(v.id);
							})
							$("#refereeB").val(arr.join(','));
						}
					})

				}
			}
		})
	// })

	// $("#refereeCBox").click(function (){
		$.ajax({
			cache: true,
			type: "get",
			url: "/jzweb/examinationBatchCom/getUserReferee",
			data: {
				track: 'C'
			},
			async: false,
			error: function (request) {
				parent.layer.alert("Connection error");
			},
			success: function (data) {

				if (data.code == 0) {
					var format = [];

					format = data.data.map(function (v) {
						if( v ){
							v.selected = false;
							v.value = v.id;
						}
						return v;
					})

					xmSelect.render({
						el: '#refereeCBox',
						autoRow: true,
						name: '',
						max: 5,
						data() {
							return format
						},
						maxMethod(seles, item) {
							layer.msg(`最多只能选择5个`)
						},
						on: function (data) {
							let arr = [];
							data.arr.map(function (v){
								arr.push(v.id);
							})
							$("#refereeC").val( arr );
							// b1race = data.arr
						}
					})

				}
			}
		})
	// })

}